package com;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.Entity;
import com.google.gson.Gson;
import com.hotel.BL.LoginLogic;

import data.AbstractData;

public class Util {

	public static int daysBetween(Date from,Date to)
	{
		Calendar cal1 = Calendar.getInstance();
        cal1.setTime(from);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(to);
        return daysBetween(cal1, cal2);
	}
	
	public static int daysBetween(Calendar startDate, Calendar endDate) {
        Calendar date = (Calendar) startDate.clone();
        int daysBetween = 0;
        while (date.before(endDate)) {
            date.add(Calendar.DAY_OF_MONTH, 1);
            daysBetween++;
        }
        return daysBetween;
    }
	public static Date addDays(Date d, long days)
    {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.DATE, (int)days);
		return c.getTime();
    }
	public static Date addMins(Date d, long mins)
    {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.MINUTE, (int)mins);
		return c.getTime();
    }
	
	public static HashMap<String,Object> toHashmap(AbstractData data)
	{
		Entity e = data.getEntity();
		HashMap<String,Object> map = new HashMap<String,Object>(e.getProperties());
		map.put("id", data.getId());
		return map;
	}
	
	public static HashMap<String,Object> toHashmapformfield(AbstractData data)
	{
		HashMap<String,Object> map2 = new HashMap<String,Object>();
		Field[] fields = data.getClass().getDeclaredFields();
		for(Field f:fields){
			f.setAccessible(true);
			try {
				map2.put(f.getName(), f.get(data));
			} catch (Exception e) {}
		}
		return map2;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object Deserialize(String data,Class classType) throws IOException, ClassNotFoundException {
		Gson gson = new Gson();
		return gson.fromJson(data, classType);
    }
	
	public static String Serialize(Object obj) throws IOException {
		Gson gson = new Gson();
		return gson.toJson(obj);
    }
	
	public static Date GetOnlyDate(Date d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.set(Calendar.HOUR_OF_DAY,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.MILLISECOND,0);
		return cal.getTime();
	}
	
	public static String checkSessionEntity(HttpServletRequest req,HttpServletResponse resp){
		HttpSession session=req.getSession(true);
		long hotelId = (Long)session.getAttribute("hotelId");
		long userId = (Long) session.getAttribute("userid");
		new LoginLogic(req,resp).UpdateLastStay(hotelId,userId);
		data.User obj =  new LoginLogic(req,resp).GetUserWithCache(userId);
		String	sessionCheck = "";
		if(obj.getSessionid()!=null)
			sessionCheck = obj.getSessionid();
		else
			sessionCheck = "";
		return sessionCheck;
	}
	
	public static int GetMin(Date d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(Calendar.MINUTE);
	}
	
	public static int GetHour(Date d){
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(Calendar.HOUR_OF_DAY);
	}
	
	public static double calPriceWithoutTax(double displayPrice,double perc,double fix,HttpSession session){
		boolean isIncludeTax = (Boolean)session.getAttribute("isIncludetax");
		return calPriceWithoutTax(displayPrice,perc,fix,isIncludeTax);
	}
	
	public static double calPriceWithoutTax(double displayPrice,double perc,double fix,boolean incTax){
		if(!incTax)	return displayPrice;
		else return(displayPrice-fix)/((perc/100)+1);
	}
	
	public static HashMap<String,Object> customCalTax(double priceWithoutTax,double perc,double fix,double discount,long amount,HttpSession session){
		boolean isIncludeTax = (Boolean)session.getAttribute("isIncludetax");
		return customCalTax(priceWithoutTax,perc,fix,discount,amount,isIncludeTax);
	}
	
	public static HashMap<String,Object> customCalTax(double priceWithoutTax,double perc,double fix,double discount,long amount,boolean incTax){
		HashMap<String,Object> rs = new HashMap<String,Object>();
		if(!incTax){
			double priceBeforeTax=(priceWithoutTax*amount)-discount;
			double tax=((priceBeforeTax*perc)/100)+(fix*amount);
			rs.put("totalPrice", priceBeforeTax+tax);
			rs.put("totalPriceBeforeTax", priceBeforeTax);
			rs.put("totalTax", tax);
			rs.put("totalPercTax", ((priceBeforeTax*perc)/100));
			rs.put("totalFixTax", (fix*amount));
			rs.put("displayPricePerUnitBeforeDiscount", priceWithoutTax);
		}
		else{
			double taxPerUnit=((priceWithoutTax*perc)/100)+fix;
			double priceIncTax=priceWithoutTax+taxPerUnit;
			double priceAfterDiscount=(priceIncTax*amount)-discount;
			double priceBeforeTax=(priceAfterDiscount-(fix*amount))/(1+(perc/100));
			double tax=((priceBeforeTax*perc)/100)+(fix*amount);
			rs.put("totalPrice", priceAfterDiscount);
			rs.put("totalPriceBeforeTax", priceBeforeTax);
			rs.put("totalTax", tax);
			rs.put("totalPercTax", ((priceBeforeTax*perc)/100));
			rs.put("totalFixTax", (fix*amount));
			rs.put("displayPricePerUnitBeforeDiscount", priceIncTax);
		}
		return rs;
	}
}
